﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PlanningGIS.Engine.Controls;
using PlanningGIS.Win.Controls;


namespace PlanningGIS.Product
{
    public partial class CTLSelectData : UserControl
    {
        /// <summary>
        /// 是否可以多选
        /// </summary>
        public bool CanSelectMultiple{get; set;}

        /// <summary>
        /// 选择数据结构类别
        /// </summary>
        public PlanningGISDataStructureType SelectDataStructureType { get; set; }

        public List<DatasetNode> SelectDatasets { get { return selectData.GetSelectNode<DatasetNode>(); } }
        public List<FeatureClassNode> SelectFeatureClasses { get { return selectData.GetSelectNode<FeatureClassNode>(); } }
        public List<CadDrawingDatasetNode> SelectCadDrawingDatasets { get { return selectData.GetSelectNode<CadDrawingDatasetNode>(); } }
        public List<LayerNode> SelectLayers { get { return selectData.GetSelectNode<LayerNode>(); } }
        public List<RasterDatasetNode> SelectRasterDatasets { get { return selectData.GetSelectNode<RasterDatasetNode>(); } }
        public List<RasterCatalogNode> SelectRasterCatalogs { get { return selectData.GetSelectNode<RasterCatalogNode>(); } }



        private OpenDataDialog selectData;

        public CTLSelectData()
        {
            CanSelectMultiple = true;
            SelectDataStructureType = PlanningGISDataStructureType.FEATURECLASS;
            selectData = new OpenDataDialog();
            InitializeComponent();
        }

        private void btnOpenData_Click(object sender, EventArgs e)
        {
             try
            {
                selectData.OnOkClick = delegate
                {
                    string strPath = selectData.FullName.TrimEnd('\\');
                    

                    switch (SelectDataStructureType)
                    {
                        case PlanningGISDataStructureType.GEODATABASE:
                            if (strPath.ToLower().EndsWith(".gdb") || strPath.ToLower().EndsWith(".mdb"))
                            {
                                if (CanSelectMultiple==true)
                                {
                                    //TODO:
                                }
                                else
                                {
                                    txtLayerPath.Text=selectData.FullName;
                                }

                                selectData.Close();
                            }
                            else
                            {
                               MsgHelper.ShowWarningMsgbox("请选择空间数据库（GDB、MDB）数据！");
                                return;
                            }
                            break;
                        case PlanningGISDataStructureType.DATASET:
                            List<DatasetNode> selectDSNode = selectData.GetSelectNode<DatasetNode>();

                            if (selectDSNode.Count > 0)
                            {
                                lstFeatLyr.Items.Clear();

                                foreach (DatasetNode dn in selectDSNode)
                                {
                                    ListViewItem listViewItemTemp = new ListViewItem(new string[]{dn.Name,dn.Path});
                                    lstFeatLyr.Items.Add(listViewItemTemp);
                                }
                                selectData.Close();
                            }
                            else
                            {
                                MsgHelper.ShowInfomationMsgbox("请选择数据集！");
                                return;
                            }
                            break;
                        case PlanningGISDataStructureType.FEATURECLASS:
                            List<FeatureClassNode> selectFCNode = selectData.GetSelectNode<FeatureClassNode>();

                            if (selectFCNode.Count > 0)
                            {
                                lstFeatLyr.Items.Clear();

                                foreach (FeatureClassNode fcn in selectFCNode)
                                {
                                    ListViewItem listViewItemTemp = new ListViewItem(new string[] { fcn.Name, fcn.Path });
                                    lstFeatLyr.Items.Add(listViewItemTemp);
                                }

                                selectData.Close();
                            }
                            else
                            {
                               MsgHelper.ShowWarningMsgbox("请选择数据要素类！");
                                return;
                            }
                            break;
                        case PlanningGISDataStructureType.FEATURE:
                            // TODO:
                            return;
                            //break;
                    }
                    
                };

                selectData.MultiSelect = CanSelectMultiple;
                selectData.ShowDialog(this);
            }
            catch (Exception ex)
            {
               MsgHelper.ShowErrorMsgbox(ex.Message);
            }
             
        }

        private void CTLSelectData_Load(object sender, EventArgs e)
        {
            if (CanSelectMultiple == false)
            {
                // 单选时不显示列表
                this.Height = 60;
            }
            else
            {
                this.Height = 240;
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            lstFeatLyr.Items.Clear();
        }
    }
}
